package info.webest.reminder;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.text.format.DateFormat;
import android.util.Log;

public class wbDate {

	/*
	private static int round(double d) {
		double dAbs = Math.abs(d);
		int i = (int) dAbs;
		double result = dAbs - i;
		if (result < 0.5) {
			return d < 0 ? -i : i;
		} else {
			return d < 0 ? -(i + 1) : i + 1;
		}
	}
	*/	
	
	public static Calendar getCalendar(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}

	public static int yearsSubstruct(Date date1, Date date2) {
		SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy");
		// Integer.parseInt(simpleDateformat.format(date1));
		return Integer.parseInt(simpleDateformat.format(date2))
				- Integer.parseInt(simpleDateformat.format(date1));
	}

	public static int yearsBetween(Date dateStart, Date dateStop) {
		Calendar a = getCalendar(dateStart);
		Calendar b = getCalendar(dateStop);
		int diff = b.get(Calendar.YEAR) - a.get(Calendar.YEAR);
		if (a.get(Calendar.MONTH) > b.get(Calendar.MONTH)
				|| (a.get(Calendar.MONTH) == b.get(Calendar.MONTH) && a
						.get(Calendar.DATE) > b.get(Calendar.DATE))) {
			diff--;
		}
		return diff;
	}

	public static int daysBetween(Date dateOne, Date dateTwo) {
		long timeOne = dateOne.getTime();
		Log.v("timeOne", "@@@ " + timeOne);
		long timeTwo = dateTwo.getTime();
		Log.v("timeTwo", "@@@ " + timeTwo);
		long oneDay = 86400000; // 1000 * 60 * 60 * 24

		Log.v("delta___", "@@@ " + (timeTwo - timeOne));

		double delta = ((timeTwo - timeOne) / oneDay);
		Log.v("delta", "@@@ " + delta);

		/*
		 * if (delta > 0) { return "dateTwo is " + delta +
		 * " days after dateOne"; } else { delta *= -1; return "dateTwo is " +
		 * delta + " days before dateOne"; }
		 */
		return (int) delta;
	}

	public static int daysBefore(int month, int day) {

		Calendar cal = Calendar.getInstance();

		Date today = cal.getTime();

		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, day);
		// cal.set(Calendar.HOUR, 0);
		// cal.set(Calendar.MINUTE, 0);
		// cal.set(Calendar.SECOND, 0);
		Date date = cal.getTime();

		Log.v("birthdate in this year",
				"@@@ " + DateFormat.format("yyyyy-MM-dd", date));

		int result = daysBetween(today, date);

		if ((result > 0) && (result < 1)) {
			result = 1;
		}

		if ((result >= 0) && (result < 7)) {
			Log.v("upcoming", "@@@ --------------------------------------");
		}
		return result;
	}

}
